package pro.shuangxi.config.mvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import pro.shuangxi.common.interceptor.MyInterceptor;

@EnableAspectJAutoProxy
@Configuration
@EnableWebMvc
@ComponentScan("pro.shuangxi.model.*.controller")
@ComponentScan("pro.shuangxi.*.controller")
public class ServletConfig implements WebMvcConfigurer{

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
    @Bean("multipartResolver")
    public CommonsMultipartResolver commonsMultipartResolver(){
      return new CommonsMultipartResolver();
    }

}
